package li.teng.android.xplanet.content;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Vector;

import li.teng.android.xplanet.XplanetConstants;
import android.content.Context;
import android.location.Location;
import android.util.Log;

public class MyPositionHelper {
	Vector<MyPosition> m_Data = new Vector<MyPosition>();
	static MyPositionHelper m_Instance;

	Context context;
	MyPositionDBAdapter adapter;
	public static MyPositionHelper getInstance(Context context){
		if(m_Instance == null)
			m_Instance = new MyPositionHelper(context);
		return m_Instance;
	}
	
	private MyPositionHelper(Context _context){
		context = _context;
		adapter = new MyPositionDBAdapter(context);
		adapter.open();		
		loadFromDatabase();
		adapter.close();
	}
	
	private void loadFromDatabase(){
//		Log.d(XplanetConstants.TAG, "adapter = " + adapter); 
//		Cursor cursor = adapter.getAllToDoItemsCursor();
//		Log.d(XplanetConstants.TAG, "cursor = " + cursor); 
//		  if (cursor.moveToFirst())
//		    do { 
//		      long id = cursor.getLong(cursor.getColumnIndex(MyPositionDBAdapter.KEY_ID));
//		      String name = cursor.getString(cursor.getColumnIndex(MyPositionDBAdapter.KEY_NAME));
//		      double logitude = cursor.getDouble(cursor.getColumnIndex(MyPositionDBAdapter.KEY_LOGITUDE));
//		      double latitude = cursor.getDouble(cursor.getColumnIndex(MyPositionDBAdapter.KEY_LATITUDE));
//		      MyPosition newItem = new MyPosition(id,name,logitude,latitude);
//		      m_Data.add(newItem);
//		      //Log.d(XplanetConstants.TAG, newItem.getId() + ":" + newItem.getName() + ":" + newItem.getLogitude());
//		    } while(cursor.moveToNext());
		try{
			InputStream fis = context.getAssets().open("locations.dat");
			BufferedReader  in = new BufferedReader (new InputStreamReader(fis,"UTF-8"));
			while(true){
		    	String line = in.readLine();
		    	Log.w(XplanetConstants.TAG, "line = " + line.length()); ;
		    	if(line == null)break;
		    	String[] paras = line.split("\t");
	            MyPosition newItem = new MyPosition(Long.parseLong(paras[0]),
	            		paras[1],Double.parseDouble(paras[2]),Double.parseDouble(paras[3]));
	            m_Data.add(newItem);
			}

	    	//	    	byte[] buffer = new byte[64];
//	    	int i = in.read(buffer);
//	    	String name = new String(buffer,"Unicode");
	    	in.close();
	    	fis.close();
	    	
	    	
		}catch(Exception ex){
			Log.w(XplanetConstants.TAG, ex.toString()); ;
		}		 
		 Log.d(XplanetConstants.TAG, "load data ok.length = " + m_Data.size()); 
	}
	
	public MyPosition getPosition(long id){
		MyPosition ret = null;
		for(int i =0; i<m_Data.size(); i++){
			if(m_Data.get(i).id == id){
				ret = m_Data.get(i);
				break;
			}
		}
		return ret;
	}
	
	public List<MyPosition> getPositionsNearBy(Location location,int distance){
		Log.d(XplanetConstants.TAG,"getPositionsNearBy" + location + "\tdistance=" + distance);
		if(location != null && distance != 0){
			Vector<MyPosition> ret = new Vector<MyPosition>();
			sortLocations(location);
			for(int i =0; i<m_Data.size(); i++){
				if(m_Data.get(i).getDistance()< distance)
					ret.add(m_Data.get(i));
			}
			return ret;
		}else{
			return m_Data;
		}
	}
	
	public void sortLocations(Location location){
		MyPosition[] tmp = new MyPosition[m_Data.size()];
		for(int i = 0; i<m_Data.size();i++){
			tmp[i] =m_Data.get(i);
		}
		for(int i= 0; i<tmp.length; i++){
			Log.d(XplanetConstants.TAG, "before sort " + i + ":" + tmp[i].getId() + ":" + tmp[i].getDistance(location));
		}
		
		float[] ret2 = new float[3];
		for(int m = 0; m<tmp.length; m++){
			for(int n = 0; n< tmp.length - m - 1; n++){
			     //args[n]=String.valueOf(a);
	             //args[n+1]=String.valueOf(b);
				float d1,d2 = 0;
				d1 = tmp[n].getDistance(location);
				d2 = tmp[n+1].getDistance(location);
				if(d2<d1){
					MyPosition tmpp = tmp[n];
					tmp[n] = tmp[n+1];
					tmp[n+1] = tmpp;
				}
			}
		}	
		
		Vector<MyPosition> newList = new Vector<MyPosition>();
		for(int i= 0; i<tmp.length; i++){
			newList.add(tmp[i]);
			Log.d(XplanetConstants.TAG, "after sort " + i + ":" + tmp[i].getId() + ":" + tmp[i].getDistance(location));
		}
		m_Data = newList;
	}
	
	public void print(){
		for(int i = 0 ;i<m_Data.size(); i++){
			Log.d(XplanetConstants.TAG, m_Data.get(i).getId() + ":" + m_Data.get(i).getName());
		}
	}
	public List<MyPosition> getAllPositions(){
		return m_Data;
	}	
}
